package com.yang.ddz.netty;


import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.*;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;

/**
 * Created by zongx on 2019/12/31.
 */

public class HttpWorkerThread implements Runnable {

    private Channel browserChannel;
    private String param;
    private String body;
    private String operation;
    private HttpVersion protocalVersion;
    private boolean keepAlive;

    private static Logger logger = Logger.getLogger(HttpWorkerThread.class);

    /**
     * @return
     * @Description: 构造方法，保存
     * @Author: zongx
     * @Date: 2019/12/31
     * @Param: protocolVersion：http协议版本
     * @Param: keepAlive：是否是长连接
     * @Param: channel：http请求来的channel
     * @Param: param：参数
     * @Param: body：请求体
     * @Param: op：操作字符
     */
    public HttpWorkerThread(HttpVersion protocolVersion, boolean keepAlive, Channel channel, String param, String body, String op) {
        this.browserChannel = channel;
        this.param = param;
        this.body = body;
        this.operation = op;
        this.keepAlive = keepAlive;
        this.protocalVersion = protocolVersion;
    }

    @Override
    public void run() {
        try {
            //获取请求内容
            JSONObject json = (JSONObject) JSONObject.parse(this.body);
            System.out.println(json);

            //构造响应体
            JSONObject result = new JSONObject();
            result.put("code", 200);
            switch (this.operation) {
                //通知当前服务邀请
                case "/add":
                    result.put("message", "增加成功");
                    break;
                //通知当前服务邀请
                case "/del":
                    result.put("message", "删除成功");
                    break;
                //通知当前服务邀请
                case "/update":
                    result.put("message", "更新成功");
                    break;
                //通知当前服务邀请
                case "/select":
                    result.put("message", "查找成功");
                    break;
            }

            //构造响应
            FullHttpResponse response = new DefaultFullHttpResponse(
                    this.protocalVersion,
                    HttpResponseStatus.OK,
                    Unpooled.wrappedBuffer(result.toJSONString().getBytes("UTF-8"))
            );
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            if (this.keepAlive) {
                response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
            }
            //返回response
            ChannelFuture future = browserChannel.writeAndFlush(response);

            // 如果不是长连接，要监听当前channel关闭
            if (!keepAlive) {
                future.addListener(ChannelFutureListener.CLOSE);
            }

            //关闭当前channel
            browserChannel.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }
}
